var userService = {
  sendVerifyCode: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/sendverifycode',
      method: 'POST',
      data: util.signature({
        mobile: options.mobile
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        console.log(statusCode)
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  bindUserWithVerifyCode: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/binduser',
      method: 'POST',
      data: util.signature({
        mobile: options.mobile,
        platformId: app.globalData.apiAppId,
        openid: options.openid,
        avatar: options.avatar,
        nickname: options.nickname,
        verifyCode: options.verifyCode
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData, {
            userId: res.data.ResultData.user_id,
            avatar: options.avatar,
            nickname: options.nickname,
          });
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  loginbyopenid: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/loginbyopenid',
      method: 'POST',
      data: util.signature({
        platformId: app.globalData.apiAppId,
        openid: options.openid
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getOpenIdByCode: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/getopenidbycode',
      method: 'POST',
      data: util.signature({
        platformId: app.globalData.apiAppId,
        authCode: options.authCode
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getCityByPos: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/getCityByPos',
      method: 'POST',
      data: util.signature({
        platformId: app.globalData.apiAppId,
        lat: options.lat,
        lng: options.lng,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  myActivity:function(options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/getMyActivityList',
      method: 'POST',
      data: util.signature({
        pageNumber: options.pageIndex,
        pageSize: options.pageSize,
        user_id: options.user_id,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function (a) {
        console.log(a);
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  setSelfInfo: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/setSelfInfo',
      method: 'POST',
      data: util.signature({
        nickname: options.nickname,
        id_card: options.id_card,
        age: options.age,
        sex: options.sex,
        born_place: options.born_place,
        school: options.school,
        record: options.record,
        profession: options.profession,
        email: options.email,
        link_name: options.link_name,
        link_phone: options.link_phone,
        home_phone: options.home_phone,
        born_address: options.born_address,
        now_address: options.now_address,
        description: options.description,
        user_id: app.globalData.apiUser
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg = errorItem[i];
              break;
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function (a) {
        console.log(a);
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getSelfInfo: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/getSelfInfo',
      method: 'POST',
      data: util.signature({
        user_id: options.user_id,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function (a) {
        console.log(a);
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  centerUserInfo: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/user/centerUserInfo',
      method: 'POST',
      data: util.signature({
        user_id: options.user_id,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function (a) {
        console.log(a);
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  }
};

module.exports = {
  sendVerifyCode: userService.sendVerifyCode,
  bindUserWithVerifyCode: userService.bindUserWithVerifyCode,
  loginbyopenid: userService.loginbyopenid,
  getOpenIdByCode: userService.getOpenIdByCode,
  getCityByPos: userService.getCityByPos,
  myActivity: userService.myActivity,
  setSelfInfo: userService.setSelfInfo,
  getSelfInfo: userService.getSelfInfo,
  centerUserInfo: userService.centerUserInfo
}